package com.wyh.contorller;

import com.wyh.entity.Houses;
import com.wyh.entity.Repairstatus;
import com.wyh.entity.RestBean;
import com.wyh.mapper.HouseMapper;
import com.wyh.mapper.UserMapper;
import com.wyh.mapper.RepiarstatusMapper;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Validated
@RestController
@RequestMapping("/api/repairstatus")
public class RepairstatusController {

    @Resource
    RepiarstatusMapper repiarstatusMapper;

    @Resource
    UserMapper userMapper;

    @Resource
    HouseMapper houseMapper;
    //根据id查询报修信息
    @PostMapping("/find")
    public RestBean<Repairstatus> findRepairStatusById(@RequestParam("id") int id){
        try {
            Repairstatus repairstatus = repiarstatusMapper.findRepairStatusById(id);
            return RestBean.success(repairstatus);
        }
        catch (Exception e){
            return RestBean.failure(500);
        }
    }
    //查询所有报修信息
    @GetMapping("/getAll")
    public RestBean<List<Repairstatus>> getAllRepairStatus(){
        try {
            return RestBean.success(repiarstatusMapper.getAllRepairStatus());
        }
        catch (Exception e){
            return RestBean.failure(500);
        }
    }
    //添加报修信息
    @PostMapping("/add")
    public RestBean<String> addRepairStatus(Repairstatus repairstatus){
        try {
            Repairstatus existingRepairstatus = repiarstatusMapper.findRepairStatusById(repairstatus.getId());
            if (existingRepairstatus == null) {
                int userid = userMapper.findUidByUsername(repairstatus.getName());
                Houses houseid = houseMapper.findHouseIdByAddress(String.valueOf(repairstatus.getAddress()));
                repairstatus.setName(String.valueOf(userid));
                repairstatus.setAddress(String.valueOf(houseid.getHouse_id()));
                repairstatus.setIssue(repairstatus.getIssue());
                repairstatus.setType(repairstatus.getType());
                repairstatus.setProgress(repairstatus.getProgress());
                repairstatus.setPriority(repairstatus.getPriority());
                repairstatus.setTime(repairstatus.getTime());
                repairstatus.setRemark(repairstatus.getRemark());
                repairstatus.setAddress(repairstatus.getAddress());
                repiarstatusMapper.addRepairStatus(repairstatus);
                return RestBean.success("添加成功");
            }
        }
        catch (Exception e){
            return RestBean.failure(500,"添加失败"+e.getMessage());
        }
        return RestBean.failure(500,"添加失败");
    }

    //修改报修信息
    @PutMapping("/update")
    public RestBean<String> updateRepairStatus(@RequestBody Repairstatus repairstatus){
        Repairstatus existingRepairstatus = repiarstatusMapper.findRepairStatusById(repairstatus.getId());
            if (existingRepairstatus != null) {
                try {
                    repairstatus.setProgress(StringUtils.hasText(repairstatus.getProgress())? repairstatus.getProgress(): existingRepairstatus.getProgress());
                    repairstatus.setRemark(StringUtils.hasText(repairstatus.getRemark())? repairstatus.getRemark(): existingRepairstatus.getRemark());
                    repiarstatusMapper.updateRepairStatus(repairstatus);
                    return RestBean.success("修改成功");
                }
                catch (Exception e){
                    return RestBean.failure(500,"修改失败"+e.getMessage());
                }
            }
        return RestBean.failure(500,"不存在该条信息");
    }
    //删除报修信息
    @DeleteMapping("/delete")
    public RestBean<String> deleteRepairStatus(@RequestParam("id") int id){
        try {
            repiarstatusMapper.deleteRepairStatus(id);
            return RestBean.success("删除成功");
        }
        catch (Exception e){
            return RestBean.failure(500,"删除失败"+e.getMessage());
        }
    }
}
